//
//  TeamManageController.m
//  TrainTree
//
//  Created by wanghui on 2017/9/11.
//  Copyright © 2017年 YouYeShu. All rights reserved.
//

#import "TeamManageController.h"
#import "TeamManagerTopCell.h"
#import "TeamManagerSectionHeader.h"
#import "TeamStatisticsCell.h"
#import "TeamDetailHeaderCell.h"
#import "EveryoneDataCell.h"
#import "CourseManagerCell.h"
#import "MineRequestManager.h"
@interface TeamManageController ()<UITableViewDelegate,UITableViewDataSource>
{
    TeamManagerSectionHeader *_headerView;
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;

/**类别*/
@property(nonatomic,assign)TeamManagerType teamType;
/**存放课程管理的数组*/
@property(nonatomic,strong)NSMutableArray *courseArray;

/**存放团队详情数组*/
@property(nonatomic,strong)NSArray *dataDetail;
/**存放团队统计数组*/
@property(nonatomic,strong)NSArray *dataReport;

@end

@implementation TeamManageController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationView.leftBtn setImage:ImageNamed(@"back") forState:UIControlStateNormal];
    [self.navigationView.leftBtn addTarget:self action:@selector(backToLastPage) forControlEvents:UIControlEventTouchUpInside];
    self.navigationView.backgroundColor = [UIColor clearColor];

    self.navigationView.titleIcon.hidden = YES;
    self.navigationView.titleLable.hidden = YES;
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.teamType = TeamManagerTypeData;
    [self.tableView registerNib:[UINib nibWithNibName:@"EveryoneDataCell" bundle:nil] forCellReuseIdentifier:@"EveryoneDataCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"CourseManagerCell" bundle:nil] forCellReuseIdentifier:@"CourseManagerCell"];
    [self requestAnalytics];
//    [self requestLessonList];
}
#pragma  mark - 请求统计数据
- (void)requestAnalytics
{
    WeakSelf(ws);
    [MineRequestManager requestTeamaAalyticsResponse:^(id JSONData, NSError *error, NSURLSessionDataTask *dataTask) {
        if ([JSONData[@"c"] intValue] == 0) {
            ws.dataReport = JSONData[@"d"][@"dataReport"];
            ws.dataDetail = JSONData[@"d"][@"dataDetail"];
            [ws.tableView reloadData];
        }
    }];
}
#pragma mark - 请求课程管理数据
- (void)requestLessonList
{
    WeakSelf(ws);
    [MineRequestManager requestTeamLessonListResponse:^(id JSONData, NSError *error, NSURLSessionDataTask *dataTask) {
        if ([JSONData[@"c"] intValue] == 0) {
            ws.courseArray = JSONData[@"d"];
            [ws.tableView reloadData];
 
        }
    }];
}


- (void)backToLastPage
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 1) {
    return 42.0f;
    }else{
        return 0.0f;
    }

}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 1) {
        _headerView = [LoadXibTool loadViewFromXibName:@"TeamManagerSectionHeader"];
        if (self.teamType == TeamManagerTypeCourse) {
            _headerView.isTouchDataBtn = NO;
        }else{
            _headerView.isTouchDataBtn = YES;
        }
        [_headerView.dataBtn addTarget:self action:@selector(clickDataBtn:) forControlEvents:UIControlEventTouchUpInside];
        [_headerView.managerBtn addTarget:self action:@selector(clickManagerBtn:) forControlEvents:UIControlEventTouchUpInside];
        return _headerView;
    }else{
        return nil;
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section == 0){
        return 1;
    } else {
        if (self.teamType == TeamManagerTypeData) {
            if (self.dataDetail.count > 0) {
                return self.dataDetail.count + 2;
            }else{
                return 3;
            }

        }else{
            return self.courseArray.count;
        }

    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        // 顶部cell
        return 212.0f / 375.0f * ScreenWidth;
    } else {
        if (self.teamType == TeamManagerTypeData) {
            if (indexPath.row == 0) {
                    // 折线图cell
                return 265.0f / 375.0f * ScreenWidth;
            } else if (indexPath.row == 1) {
                    // 团队详情及表头
                return 86.0f;
            } else {
                return 36.0f;
            }
        } else {
            return 60.f;
        }
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        TeamManagerTopCell *cell = [LoadXibTool loadViewFromXibName:@"TeamManagerTopCell"];
        cell.userNameLabel.text = self.userName;
        [cell.icon sd_setImageWithURL:[NSURL URLWithString:self.icon] placeholderImage:ImageNamed(@"ty_pic")];
        cell.companyNameLabel.text = self.companyName;
        return cell;
    } else {
        if (self.teamType == TeamManagerTypeData) {
            if (indexPath.row == 0) {
                TeamStatisticsCell *cell = [LoadXibTool loadViewFromXibName:@"TeamStatisticsCell"];
                [cell configWithArray:self.dataReport];
                return cell;
            } else if (indexPath.row == 1) {
                    //数据表头
                TeamDetailHeaderCell *cell = [LoadXibTool loadViewFromXibName:@"TeamDetailHeaderCell"];
                return cell;
            } else {
                    // 各人数据
                EveryoneDataCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EveryoneDataCell"];
                if (self.dataDetail.count > 0) {
                    NSDictionary *dic = self.dataDetail[indexPath.row - 2];
                    cell.name.text = dic[@"userName"];
                    cell.studiedCount.text = [NSString stringWithFormat:@"%@",dic[@"studiedCount"]];
                    cell.studyingCount.text = [NSString stringWithFormat:@"%@",dic[@"studyingCount"]];
                    cell.studyTimeCount.text = [NSString stringWithFormat:@"%@",dic[@"studyTimeCount"]];
                }
                return cell;
            }
        } else if (self.teamType == TeamManagerTypeCourse) {
            CourseManagerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CourseManagerCell"];
            if (self.courseArray.count > 0) {
                [cell configWithData:self.courseArray[indexPath.row]];
            }
            return cell;
        }else{
            return nil;
        }
    }
}

#pragma  mark - 点击了数据统计按钮
- (void)clickDataBtn:(UIButton *)dataBtn
{
    _headerView.isTouchDataBtn = YES;
    self.teamType = TeamManagerTypeData;
    [self requestAnalytics];
}
#pragma  mark - 点击了课程管理按钮
- (void)clickManagerBtn:(UIButton *)managerBtn
{
    _headerView.isTouchDataBtn = NO;
    self.teamType = TeamManagerTypeCourse;
    [self  requestLessonList];

}

- (NSMutableArray *)courseArray
{
    return _courseArray ?: (_courseArray = [NSMutableArray array]);
}
- (NSArray *)dataDetail
{
    return _dataDetail ?: (_dataDetail = [NSArray array]);
}

- (NSArray *)dataReport
{
    return _dataReport ?: (_dataReport = [NSArray array]);
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
